﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FolderCopyActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property OriginalFolder As InArgument(Of String)
    Public Property TargetFolder As InArgument(Of String)
    Public Property OverrideFile As InArgument(Of Boolean)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncCopyFolderDelegate(ByVal source As String, ByVal target As String, ByVal overrideFile As Boolean) As Boolean

    Public Sub New()
        Me.DisplayName = My.Resources.FolderCopyDisplayName
    End Sub

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Dim asyncExecute = New asyncCopyFolderDelegate(AddressOf FolderCopy)
        Dim sourceFolder = context.GetValue(Me.OriginalFolder)
        Dim targetFolder = context.GetValue(Me.TargetFolder)
        Dim overrideFile = context.GetValue(Me.OverrideFile)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(sourceFolder, targetFolder, overrideFile, callback, state)
    End Function

    Protected Overrides Sub EndExecute(context As System.Activities.AsyncCodeActivityContext, result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncCopyFolderDelegate)
        Dim copyResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, copyResult)
    End Sub

    Private Function FolderCopy(ByVal source As String, ByVal target As String, ByVal overrideFile As Boolean) As Boolean
        Dim copyResult = False
        Try
            If Not System.IO.Directory.Exists(source) Then Exit Try '対象フォルダが存在しない場合はエラー
            My.Computer.FileSystem.CopyDirectory(source, target, overrideFile)
            copyResult = True
        Catch ex As Exception

        End Try
        Return copyResult
    End Function



    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        'FolderRename Activity
        build.AddCustomAttributes(GetType(FolderCopyActivity),
                                              New ToolboxBitmapAttribute(GetType(FolderCopyActivity), "FolderCopy.png"))
        build.AddCustomAttributes(GetType(FolderCopyActivity), "OriginalFolder",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderCopyCategory),
                                              New DisplayNameAttribute(My.Resources.FolderCopyOriginalFolderDisplay),
                                              New DescriptionAttribute(My.Resources.FolderCopyOriginalFolderDescription))
        build.AddCustomAttributes(GetType(FolderCopyActivity), "TargetFolder",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderCopyCategory),
                                              New DisplayNameAttribute(My.Resources.FolderCopyTargetFolderDisplay),
                                              New DescriptionAttribute(My.Resources.FolderCopyTargetFolderDescription))
        build.AddCustomAttributes(GetType(FolderCopyActivity), "OverrideFile",
                                              New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderCopyCategory),
                                              New DisplayNameAttribute(My.Resources.FolderCopyOverrideFileDisplay),
                                              New DescriptionAttribute(My.Resources.FolderCopyOverrideFileDescription))
        build.AddCustomAttributes(GetType(FolderCopyActivity), "Result",
                                              New CategoryAttribute(My.Resources.FolderCopyCategory),
                                              New DisplayNameAttribute(My.Resources.FolderCopyResultDisplay),
                                              New DescriptionAttribute(My.Resources.FolderCopyResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

End Class
